import {asyncRoutes, defaultRoutes} from "@/router";
import {removeToken} from "@/common/token";

const state = {
    permission: [],
}


const getters = {
    permission: (state) => state.permission
}

const actions = {
    FILTERMENU({commit}, roles) {
        return new Promise(async (resolve, reject) => {
            //取出角色
            let rolesName = roles.map(v => v.name); //['input'];
            let home = defaultRoutes.filter(v => v.path == '/home')[0];

            rolesName.push('approve')

            if (rolesName.includes('administrator')) {
                home.children = asyncRoutes;
            } else {
                //过滤数据
                // let filterData = asyncRoutes.filter(v=>v.meta.roles[0]==[...rolesName]); //过滤数据
                // home.children = asyncRoutes.filter(v => rolesName.some(d => v.meta.roles.includes(d)));

                home.children = asyncRoutes.filter(v => rolesName.some(d => {
                    let obj = v.meta.roles || []
                    return obj.includes(d)
                }));
            }

            let _routes = [home] || [];
            resolve(_routes)
            commit('SETPERMISSION', _routes)
        })
    },
}


const mutations = {
    SETPERMISSION: (state, d) => {
        state.permission = d
    }
}

export default {
    state,
    getters,
    actions,
    mutations,
}